﻿using System;
using System.Linq;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using System.Web.UI.WebControls;

namespace arb.SharePoint.SecureSilverlight
{
    public class SilverlightEditorPart : EditorPart
    {
        public ListBox SilverlightFiles;
        public DropDownList Libs;
        public TextBox TbHeight;
        public TextBox TbWidth;
        public TextBox TbEmail;
         
        public SilverlightEditorPart()
        {
            ChromeType = PartChromeType.None;
        }

        public override void SyncChanges()
        {
        }


        public override bool ApplyChanges()
        {
            var wp = (SilverlightWebPartHost)WebPartToEdit;
            wp.SlDocLibUrl = Libs.SelectedItem.Value;
            wp.SlFile = SilverlightFiles.SelectedItem.Text;
            wp.SlHeight = Convert.ToInt32(TbHeight.Text);
            wp.SlWidth = Convert.ToInt32(TbWidth.Text);
            if (TbEmail.ReadOnly)
            {
                return true;
            }
            if (Builders.IsValidEmail(TbEmail.Text))
            {
                wp.EmailAddress = TbEmail.Text;
            }
            else
            {
                return false;
            }
            return true;
        }

        protected override void CreateChildControls()
        {
            SPWeb web = SPContext.Current.Site.RootWeb;

            Panel controlHolder = new Panel();

            controlHolder.Controls.Add(Builders.GenerateToolPartHeader());

            Libs = new DropDownList {AutoPostBack = true};
            Libs.SelectedIndexChanged += LibsSelectedIndexChanged;

            foreach (SPDocumentLibrary lib in web.GetAllNonSystemDocLibs())
            {
                Libs.Items.Add(new ListItem(lib.Title, string.Format("{0}/{1}", web.Url, lib.RootFolder.Url)));
            }

            controlHolder.InsertBreak();
            Label lblLibs = new Label {Text = Constants.DocumentLibraryLabel};
            controlHolder.Controls.Add(lblLibs);
            controlHolder.Controls.Add(Libs);

            controlHolder.InsertBreak();

            Label lblFiles = new Label {Text = Constants.SilverlightFilesLabel};
            controlHolder.Controls.Add(lblFiles);
            SilverlightFiles = new ListBox();

            SilverlightFiles.Width = Unit.Pixel(200);
            controlHolder.Controls.Add(SilverlightFiles);
            PopulateIntialItems(Libs, SilverlightFiles, Libs.Items[0].Value);
            controlHolder.InsertBreak();

            Label lblHeight = new Label {Text = Constants.HeightSettingsLabel};
            controlHolder.Controls.Add(lblHeight);
            TbHeight = new TextBox {Text = Constants.PromptHeightLabel};
            controlHolder.Controls.Add(TbHeight);

            controlHolder.InsertBreak();

            Label lblWidth = new Label {Text = Constants.WidthSettingsLabel};
            controlHolder.Controls.Add(lblWidth);
            TbWidth = new TextBox {Text = Constants.PromptWidthLabel};
            controlHolder.Controls.Add(TbWidth);

            controlHolder.InsertBreak();

            CheckBox cbSendEmail = new CheckBox {Text = Constants.PromptAlertsLabel, AutoPostBack = true};
            cbSendEmail.CheckedChanged += CbSendEmailCheckedChanged;
            controlHolder.Controls.Add(cbSendEmail);

            controlHolder.InsertBreak();

            Label lblEmail = new Label {Text = Constants.EmailAlertsLabel};
            controlHolder.Controls.Add(lblEmail);
            TbEmail = new TextBox {ReadOnly = true, Text = Constants.PromptEmailLabel};
            controlHolder.Controls.Add(TbEmail);

            Controls.Add(controlHolder);
            base.CreateChildControls();
        }

        void CbSendEmailCheckedChanged(object sender, EventArgs e)
        {
            TbEmail.ReadOnly = false;
        }
     
        public static void PopulateIntialItems(DropDownList ddlBox, ListBox listBox, string value)
        {
            foreach (SPFile file in SPContext.Current.Site.RootWeb.GetAllNonSystemDocLibs().Where
               (lib => value == string.Format("{0}/{1}", SPContext.Current.Site.RootWeb.Url, lib.RootFolder.Url)).
               Select(lib => lib.RootFolder.Files).SelectMany(files => files.Cast<SPFile>().
                   Where(file => file.Name.Contains(".xap"))))
            {
                listBox.Items.Add(file.Name);
            }

        }

        public void LibsSelectedIndexChanged(object sender, EventArgs e)
        {
            SilverlightFiles.Items.Clear();
            var box = (DropDownList) sender;
            PopulateIntialItems(Libs, SilverlightFiles, box.SelectedItem.Value);
        }
    }
}
